package hiep.examandroidbasic;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<SinhVienEntity> {

	Activity context;
	int layoutId;
	List<SinhVienEntity> list;

	public MyArrayAdapter(Context context, int layoutId,
			List<SinhVienEntity> objects) {
		super(context, layoutId, objects);
		this.context = (Activity) context;
		this.layoutId = layoutId;
		this.list = objects;
	}

	@SuppressLint("ViewHolder")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		ViewHolder viewHolder = null;

		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getLayoutInflater();
			rowView = inflater.inflate(R.layout.layout_list_sinhvien, parent,
					false);
			viewHolder = new ViewHolder();
			viewHolder.id = (TextView) rowView.findViewById(R.id.tvIDitem);
			viewHolder.name = (TextView) rowView.findViewById(R.id.tvNameitem);
			viewHolder.age = (TextView) rowView.findViewById(R.id.tvAgeitem);
			viewHolder.country = (TextView) rowView
					.findViewById(R.id.tvCountryitem);
			rowView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		SinhVienEntity sinhvienentity = list.get(position);
		viewHolder.id.setText(sinhvienentity.getId() + "");
		viewHolder.name.setText(sinhvienentity.getName());
		viewHolder.age.setText(sinhvienentity.getAge() + "");
		viewHolder.country.setText(sinhvienentity.getCountry());
		return rowView;
	}

	static class ViewHolder {
		TextView id;
		TextView name;
		TextView age;
		TextView country;
	}
}
